// pages/login/selectIdentity/selectIdentity.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentType: -1,
    identities: [{
        name: "",
        gender: 1,
        identity: "父亲",
        avatarUrl: "/static/images/father.png",
      }, {
        name: "",
        gender: 0,
        identity: "母亲",
        avatarUrl: "/static/images/mother.png",
      },
      {
        name: "",
        gender: 1,
        identity: "爷爷",
        avatarUrl: "/static/images/grandPa.png",
      }, {
        name: "",
        gender: 0,
        identity: "奶奶",
        avatarUrl: "/static/images/grandMa",
      }, {
        name: "",
        gender: 1,
        identity: "外公",
        avatarUrl: "/static/images/grandPapa.png",
      }, {
        name: "",
        gender: 0,
        identity: "外婆",
        avatarUrl: "/static/images/grandMama.png",
      }, {
        name: "",
        gender: -1,
        identity: "其他监护人",
        avatarUrl: "/static/images/other.png",
      }
    ],
    currentIdentities: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    wx.getStorage({
      key: 'parentsInfo',
      success: function(res) {
        wx.removeStorageSync("parentsInfo");
        let parents = [];
        let hasOther = false;
        res.data.forEach((item, index) => {
          that.data.identities.forEach(option => {
            if (item.relationName == "其他") hasOther = true;
            if (item.relationName == option.identity) {
              parents.push({
                name: item.name,
                gender: option.gender,
                stdtPantId: item.stdtPantId,
                identity: item.relationName,
                type: index,
                avatarUrl: option.avatarUrl
              })
            }

          })
        })

        if (!hasOther) {
          parents.push({
            name: "",
            gender: "",
            stdtPantId: "",
            identity: "其他监护人",
            type: parents.length,
            avatarUrl: "/static/images/other.png"
          })
        }
        that.setData({
          currentIdentities: parents
        })
      },
    })
  },
  selectIdentity: function(e) {
    let index = e.currentTarget.dataset.type;
    this.setData({
      currentType: index
    })

    wx.setStorage({ 
      key: 'currentParent',
      data: this.data.currentIdentities[index]
    })

    if (!this.data.currentIdentities[index].name) {
      //选择的是非后台添加的其他监护人
      wx.navigateTo({
        url: '/pages/login/bindOther/bindOther'
      })

    } else {
      wx.navigateTo({
        url: '/pages/login/bindParent/bindParent'
      })
    }
  }
})